package com.spring.sunflower.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.spring.sunflower.R;
import com.zyyoona7.picker.ex.DayWheelView;
import com.zyyoona7.picker.ex.MonthWheelView;
import com.zyyoona7.picker.ex.YearWheelView;
import com.zyyoona7.wheel.WheelView;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

import static com.spring.sunflower.util.AppUtils.getAstro;

/**
 * Created by Kevin on 2021/6/29<br/>
 * <p>
 * Describe:<br/>
 */
public class UserInfoBirthdayDialog extends BottomPopupView {
    private TextView tvOK, tvCancel, tvYear, tvMonth, tvDay;
    private YearWheelView yearWheelView;
    private MonthWheelView monthWheelView;
    private DayWheelView dayWheelView;
    private OnBottomSelectListener listener;

    public UserInfoBirthdayDialog(@NonNull @NotNull Context context, OnBottomSelectListener listener) {
        super(context);
        this.listener = listener;
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.custom_bottom_dialog_birthday;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        tvOK = findViewById(R.id.tvOK);
        tvCancel = findViewById(R.id.tvCancel);
        yearWheelView = findViewById(R.id.wv_year);
        monthWheelView = findViewById(R.id.wv_month);
        dayWheelView = findViewById(R.id.wv_day);
        tvYear = findViewById(R.id.tv_year);
        tvMonth = findViewById(R.id.tv_month);
        tvDay = findViewById(R.id.tv_day);
        Calendar maxCalendar = Calendar.getInstance();
        maxCalendar.set(Calendar.YEAR, 2003);
        yearWheelView.setMaxYear(2003);
        yearWheelView.setYearRange(1949, 2003);
        yearWheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener<Integer>() {
            @Override
            public void onItemSelected(WheelView<Integer> wheelView, Integer integer, int i) {
                dayWheelView.setYear(integer);
            }
        });
        monthWheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener<Integer>() {
            @Override
            public void onItemSelected(WheelView<Integer> wheelView, Integer integer, int i) {
                dayWheelView.setMonth(integer);
            }
        });
        dayWheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener<Integer>() {
            @Override
            public void onItemSelected(WheelView<Integer> wheelView, Integer integer, int i) {

            }
        });
        tvOK.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int year = dayWheelView.getYear();
                int month = dayWheelView.getMonth();
                int selectedDay = dayWheelView.getSelectedDay();
                if (listener != null) {
                    listener.onBottomSelect(year + "-" + month + "-" + selectedDay, getAstro(month, selectedDay));
                }
                dismiss();
            }
        });
        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * 0.5f);
    }

//    @Override
//    public int getMinimumHeight() {
//        return (int) (XPopupUtils.getScreenHeight(getContext()) * 0.3f);
//    }


}
